package study.javase.thread;

import java.util.concurrent.*;

/**
 * 多线程开启方式（4） 通过线程池的方式
 * 步骤
 *
 * @author LiShaowen
 * @date 2020/9/4 15:06
 */
public class StartCallableThreadPool implements Callable<String> {
    @Override
    public String  call() throws Exception {
        for ( int x = 0 ; x < 10 ; x ++ ) {
            System.out.println(Thread.currentThread().getName() + " : "+x);
        }
        return "线程执行完毕！";
    }

    public static void main(String[] args) throws Exception {
        //创建线程池对象
        ExecutorService service = Executors.newFixedThreadPool(2);
        // 提交执行
        Future<String> submit = service.submit(new StartCallableThreadPool());

        Future<String> submit2 = service.submit(new StartCallableThreadPool());
        // 获取结果
        System.out.println(submit.get());
        System.out.println(submit2.get());
        // 关闭服务
        service.shutdownNow();


    }
}
